<?php
namespace Admin\Controller;

use Think\Controller;

class DifferenceController extends RightController
{
    // 加载初始化内容页,并检索
    public function index()
    {
        //判断分页时搜索条件
        if (I("get.p")) {
            $where = session("QG_SEARCH_WHERE_difference");
            $params = session("QG_SEARCH_PARAMS_difference");
            $where = empty($where) ? "1" : $where;
            $pageSize = session("QG_SEARCH_SIZE_difference");
        } else {
            //清除session存储
            session("QG_SEARCH_WHERE_difference", null);
            session("QG_SEARCH_PARAMS_difference", null);
            session("QG_SEARCH_SIZE_difference", null);
            $params = I("post.");
            $pageSize = I("post.pageSize");

            $where = "1";
        }

        // POST数据提交，重新组合检索条件
        if (IS_POST) {
            unset($_GET["p"]);
            $where = $this->searchCondition();
        }

        // 获取登录权限
        $account = $this->getAccount();

        // 如果是分公司
        if ($this->isCompany()) {
            $where .= " AND fAgen.companyid = '" . $account['companyid'] . "'";
        }
		
		// 分页输入
        if(empty($pageSize)){
            $pageSize = 10;
        }

        // 总条数
        $count = M('difference')
            ->alias("t")
            ->join("LEFT JOIN agen arAgen ON arAgen.id = t.arrearsId")
            ->join("LEFT JOIN agen acAgen ON acAgen.id = t.accountId")
            ->join("LEFT JOIN agen fAgen ON fAgen.id = t.fromagenid")
            ->where($where)
            ->count();
        $page = new \Admin\Model\Page($count, $pageSize);
        $show = $page->show();

        // 进行分页数据查询
        $list = M('difference')
            ->alias("t")
            ->join("LEFT JOIN agen arAgen ON arAgen.id = t.arrearsId")
            ->join("LEFT JOIN agen acAgen ON acAgen.id = t.accountId")
            ->join("LEFT JOIN agen fAgen ON fAgen.id = t.fromagenid")
            ->field("t.*,arAgen.account as arrearsAccount,arAgen.name as arrearsName,acAgen.account as accountAccount,acAgen.name as accountName,fAgen.account as fromagenAccount,fAgen.name as fromagenName")
            ->where($where)
            ->order('t.id DESC')
            ->limit($page->firstRow . ',' . $page->listRows)->select();
			
		// 统计
        $sum = M('difference')
            ->alias("t")
            ->join("LEFT JOIN agen arAgen ON arAgen.id = t.arrearsId")
            ->join("LEFT JOIN agen acAgen ON acAgen.id = t.accountId")
            ->join("LEFT JOIN agen fAgen ON fAgen.id = t.fromagenid")
            ->field("sum(money) as allMoney")
            ->where($where)
            ->order('t.id DESC')
            ->find();

        // 渲染查询条件到session
        session("QG_SEARCH_WHERE_difference", $where);
		session("QG_SEARCH_PARAMS_difference", $params);
		session("QG_SEARCH_SIZE_difference", $pageSize);
		
		// 输出数据
        $this->assign("params", $params);
        $this->assign('list', $list);
        $this->assign('sum', $sum);
		$this->assign('lists', array(
            "page" => getDropdownList("pageSize" , "" ,false),
            "differenceStatus" => getDropdownList("differenceStatus"),
            "companyList" => $this->getBranchcompanyList()
        ));
        $this->assign('page', $show);
        $this->display();
    }

    // 搜索条件
    public function searchCondition()
    {
        $where = "1";

        // 来源代理
        if (I('post.fromAgenAccount')) {
            // 精确
            if (I('post.fromAgenAccountCondition') == 1) {
                $where .= " AND fAgen.account ='" . I("post.fromAgenAccount") . "'";
            } else {
                $where .= " AND fAgen.account LIKE '%" . I("post.fromAgenAccount") . "%'";
            }
        }

        // 欠款方
        if (I('post.arrearsAccount')) {
            // 精确
            if (I('post.arrearsAccountCondition') == 1) {
                $where .= " AND arAgen.account ='" . I("post.arrearsAccount") . "'";
            } else {
                $where .= " AND arAgen.account LIKE '%" . I("post.arrearsAccount") . "%'";
            }
        }

        // 欠谁款
        if (I('post.accountAccount')) {
            // 精确
            if (I('post.accountAccountCondition') == 1) {
                $where .= " AND acAgen.account ='" . I("post.accountAccount") . "'";
            } else {
                $where .= " AND acAgen.account LIKE '%" . I("post.accountAccount") . "%'";
            }
        }

        // 来源ID
        if (I('post.sourceid')) {
            $where .= " AND t.sourceId='" . I("post.sourceid") . "'";
        }

        // 金额
        if (I('post.moneyStart') || I('post.moneyEnd')) {
            // 开始
            if(I('post.moneyStart')){
                $where .= " AND t.money>='" . I("post.moneyStart") . "'";
            }
            // 结束
            if(I('post.moneyEnd')){
                $where .= " AND t.money<='" . I("post.moneyEnd") . "'";
            }
        }

        // 状态
        if (I('post.status')) {
            $where .= " AND t.status='" . I("post.status") . "'";
        }

        // 创建时间
        if (I('post.createtimeStart') || I('post.createtimeEnd')) {
            // 起始日期
            if(I('post.createtimeStart')){
                $where .= " AND t.createTime>='" . I("post.createtimeStart") . "'";
            }
            // 结束日期
            if(I('post.createtimeEnd')){
                $where .= " AND t.createTime<='" . I("post.createtimeEnd") . " 23:59:59'";
            }
        }

        // 更新时间
        if (I('post.updatetimeStart') || I('post.updatetimeEnd')) {
            // 起始日期
            if(I('post.updatetimeStart')){
                $where .= " AND t.updateTime>='" . I("post.updatetimeStart") . "'";
            }
            // 结束日期
            if(I('post.updatetimeEnd')){
                $where .= " AND t.updateTime<='" . I("post.updatetimeEnd") . " 23:59:59'";
            }
        }

        return $where;
    }

    // 更新状态
    public function confirm(){
        if (IS_POST) {
            // ID
            $id = intval(I("post.id"));

            // 没有ID
            if(empty($id)){
                $this->ajaxError("错误！");
            }

            // 更新数据
            $data['status'] = "2";
            $data['updateTime'] = now_datetime();
            $result = M('difference')->where(array("id" => $id))->save($data);

            // 如果删除成功
            if ($result) {
                $this->ajaxSuccess("更新成功！");
            } else {
                $this->ajaxError("更新失败！");
            }
        }
    }
}